package me.jiatao.wan.common.binding

import android.app.Activity
import android.content.Intent
import android.graphics.drawable.Drawable
import android.view.View
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.databinding.BindingAdapter
import androidx.swiperefreshlayout.widget.CircularProgressDrawable
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import coil.load
import com.google.android.material.button.MaterialButton
import com.google.android.material.progressindicator.CircularProgressIndicator
import me.jiatao.wan.R
import me.jiatao.wan.common.ext.dip2px
import me.jiatao.wan.ui.login.LoginActivity
import timber.log.Timber

/**
 * @Author: taojia
 * @CreateDate: 2021/8/25 2:22 下午
 * @Version: 1.0
 * @Description: DataBinding Adapter
 */

@BindingAdapter("bindingVisible")
fun bindingVisible(view: View, isVisible: Boolean) {
    view.isVisible = isVisible
}

@BindingAdapter("bindingBanner")
fun bindingBanner(imageView: ImageView, url: String) {
    imageView.load(url) {
        placeholder(R.drawable.ic_launcher_foreground)
            .error(R.drawable.ic_launcher_foreground)
//            .listener(
//                onSuccess = { _, _ ->
//                    imageView.scaleType = ImageView.ScaleType.FIT_XY
//                },
//                onError = { _, _ ->
//                    imageView.scaleType = ImageView.ScaleType.CENTER
//                }
//            )
    }
}

@BindingAdapter("bindingAvatar")
fun bindingAvatar(imageView: ImageView, url: String) {
    val sizeDP = imageView.context.dip2px(20f)
    imageView.load(url) {
        placeholder(R.color.purple_700)
        size(sizeDP, sizeDP)
    }
}

@BindingAdapter("bindingUserAvatar")
fun bindingUserAvatar(imageView: ImageView, url: String) {
    if (url.isEmpty()) {
        imageView.setImageResource(R.drawable.ic_launcher_foreground)
        return
    }

    val sizeDP = imageView.context.dip2px(80f)
    imageView.load(url) {
        placeholder(R.drawable.ic_launcher_foreground)
        size(sizeDP, sizeDP)
    }
}

@BindingAdapter("bindingSwipeLoading")
fun bindingSwipeLoading(swipe: SwipeRefreshLayout, isLoading: Boolean) {
    Timber.tag("bindingLoading").e(" isLoading = ${isLoading}")
    swipe.isRefreshing = isLoading
    if (!isLoading) swipe.isEnabled = false
}

@BindingAdapter("bindingIndicatorLoading")
fun bindingIndicatorLoading(indicator: CircularProgressIndicator, isLoading: Boolean) {
    Timber.tag("bindingIndicatorLoading").e(" isLoading = ${isLoading}")
    indicator.isVisible = isLoading
    indicator.isEnabled = isLoading
}


@BindingAdapter("bindingFinish")
fun bindingFinish(view: View, finish: Boolean) {
    val ctx = view.context
    if (ctx is Activity && finish) {
        view.setOnClickListener { ctx.finish() }
    }
}

@BindingAdapter("bindingLogin")
fun bindingLogin(view: View, start: Boolean) {
    view.setOnClickListener {
        val ctx = view.context
        if (ctx is Activity && start) {
            ctx.startActivity(Intent(ctx, LoginActivity::class.java))
        }
    }
}

@BindingAdapter("showProgress")
fun MaterialButton.setShowProgress(showProgress: Boolean) {
    icon = if (showProgress) {
        CircularProgressDrawable(context!!).apply {
            setStyle(CircularProgressDrawable.DEFAULT)
            setColorSchemeColors(ContextCompat.getColor(context!!, R.color.white))
            start()
        }
    } else null

    if (icon != null) { // callback to redraw button icon
        icon.callback = object : Drawable.Callback {
            override fun unscheduleDrawable(who: Drawable, what: Runnable) {
            }

            override fun invalidateDrawable(who: Drawable) {
                this@setShowProgress.invalidate()
            }

            override fun scheduleDrawable(who: Drawable, what: Runnable, `when`: Long) {
            }
        }
    }
}